package com.tt.plat8.bootstrap;

import org.osgi.framework.Bundle;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * UserBean: tangtao
 * Date: 14-1-20
 * Time: 上午10:38
 * To change this template use File | Settings | File Templates.
 */
public class ApplicationResourcesServlet extends HttpServlet implements BundleActivator {

    private static List<Bundle> vaadinBundles = new ArrayList<Bundle>();

   // private static final String VAADIN_BUNDLE_SYMBOLIC_NAME = "com.vaadin";

    private String contextPath="VAADIN";

    private static Bundle vaadinServerBundle;

    private static Bundle vaadinClientCompiledBundle;

    private static Bundle vaadinThemesBundle;

    public static final String CONTEXT_PATH_PROPERTY = "contextPath";

    private static final String VAADIN_SERVER_BUNDLE = "com.vaadin.server";

    private static final String VAADIN_THEMES_BUNDLE = "com.vaadin.themes";

    private static final String VAADIN_CLIENT_COMPILED_BUNDLE = "com.vaadin.client-compiled";

    @Override
    public void start(BundleContext bundleContext) throws Exception {
        //To change body of implemented methods use File | Settings | File Templates.


        for (Bundle bundle : bundleContext.getBundles()) {
            if (VAADIN_SERVER_BUNDLE.equals(bundle.getSymbolicName())) {
                this.vaadinServerBundle = bundle;
            }
            if (VAADIN_THEMES_BUNDLE.equals(bundle.getSymbolicName())) {
                this.vaadinThemesBundle = bundle;
            }
            if (VAADIN_CLIENT_COMPILED_BUNDLE.equals(bundle.getSymbolicName())) {
                this.vaadinClientCompiledBundle = bundle;
            }
        }
    }

    @Override
    public void stop(BundleContext bundleContext) throws Exception {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String path = req.getPathInfo();
        String resourcePath = this.contextPath + path;

        URL u = this.vaadinServerBundle.getResource(resourcePath);

        if (u == null) {
            u = this.vaadinThemesBundle.getResource(resourcePath);
            if (u == null) {
                u = this.vaadinClientCompiledBundle.getResource(resourcePath);
                if (u == null) {
                    resp.sendError(HttpServletResponse.SC_NOT_FOUND,
                            String.format("Cannot find a Vaadin resource under the path [%s].", resourcePath));
                    return;
                }
            }
        }

        copy(u.openStream(), resp.getOutputStream());
    }

    void copy(InputStream from, OutputStream to) throws IOException {
        byte[] buffer = new byte[1024];
        int read = 0;
        while ((read = from.read(buffer)) != -1) {
            to.write(buffer, 0, read);
        }
    }


}
